home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro22 / cbimport.c < prev    next >
Text File  |  1990-06-20  |  4KB  |  149 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbimport.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. #include <stdio.h>
  10. /*#include <stdlib.h>*/
  11.  
  12. /* non-ansi headers */
  13. #include <bool.h>
  14.  
  15. /* local headers */
  16. #include "cbase_.h"
  17.  
  18. /*man---------------------------------------------------------------------------
  19. NAME
  20.      cbimport - cbase import
  21.  
  22. SYNOPSIS
  23.      #include <cbase.h>
  24.  
  25.      int cbimport(cbp, filename)
  26.      cbase_t *cbp;
  27.      const char *filename;
  28.  
  29. DESCRIPTION
  30.      The cbimport function imports all records from a printable file
  31.      to cbase cbp.  filename points to a character string that
  32.      contains the name of the printable file.  See cbexport for a
  33.      definition of the file format.
  34.  
  35.      If a record containing an illegal duplicate key is encountered
  36.      during the import, that record is skipped and the import
  37.      continues with the subsequent record.  On successful completion
  38.      of the remainder of the import, a value of -1 is returned with
  39.      errno set to CBEDUP.  Whether or not the calling program should
  40.      treat this as an error condition is application dependent.
  41.  
  42.      cbimport will fail if one or more of the following is true:
  43.  
  44.      [EINVAL]       cbp is not a valid cbase pointer.
  45.      [EINVAL]       filename is the NULL pointer.
  46.      [ENOENT]       filename does not exist.
  47.      [CBEDUP]       An illegal duplicate key was encountered.
  48.      [CBEPRFILE]    Error reading from printable file.
  49.      [CBEPRFILE]    Invalid printable file format.
  50.  
  51. SEE ALSO
  52.      cbexport.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. int cbimport(cbp, filename)
  60. cbase_t *cbp;
  61. const char *filename;
  62. {
  63.     FILE *    fp    = NULL;        /* import stream */
  64.     void *    buf    = NULL;        /* input buffer */
  65.     bool    dupflag    = FALSE;    /* illegal duplicate key flag */
  66.     int    field    = 0;        /* field number */
  67.     size_t    fldos    = 0;        /* field offset */
  68.     size_t    fldlen    = 0;        /* field length */
  69.     int    fldtype    = 0;        /* field data type */
  70.     int    terrno    = 0;        /* tmp errno */
  71.  
  72.     /* validate arguments */
  73.     if (!cb_valid(cbp) || filename == NULL) {
  74.         errno = EINVAL;
  75.         return -1;
  76.     }
  77.  
  78.     /* open file for reading */
  79.     fp = fopen(filename, "r");
  80.     if (fp == NULL) {
  81.         return -1;
  82.     }
  83.  
  84.     /* import all records from printable file */
  85.     buf = calloc((size_t)1, cbrecsize(cbp));
  86.     if (buf == NULL) {
  87.         CBEPRINT;
  88.         fclose(fp);
  89.         errno = ENOMEM;
  90.         return -1;
  91.     }
  92.     while (!feof(fp)) {
  93.         if (ferror(fp)) {
  94.             CBEPRINT;
  95.             free(buf);
  96.             fclose(fp);
  97.             return -1;
  98.         }
  99.         memset(buf, 0, cbrecsize(cbp));
  100.         /* input each field of new record */
  101.         for (field = 0; field < cbp->fldc; ++field) {
  102.             fldos = cbp->fldv[field].offset;
  103.             fldlen = cbp->fldv[field].len;
  104.             fldtype = cbp->fldv[field].type;
  105.             /* read field from printable file */
  106.             if ((*cbimpv[fldtype])(fp, (char *)buf + fldos, fldlen) == -1) {
  107.                 CBEPRINT;
  108.                 free(buf);
  109.                 fclose(fp);
  110.                 errno = CBEPRFILE;
  111.                 return -1;
  112.             }
  113.             if (feof(fp)) break;
  114.         }
  115.         if (feof(fp)) {
  116.             break;
  117.         }
  118.         /* add record to database */
  119.         if (cbinsert(cbp, buf) == -1) {
  120.             if (errno == CBEDUP) {
  121.                 dupflag = TRUE;
  122.             } else {
  123.                 terrno = errno;
  124.                 free(buf);
  125.                 fclose(fp);
  126.                 errno = terrno;
  127.                 return -1;
  128.             }
  129.         }
  130.     }
  131.     free(buf);
  132.     buf = NULL;
  133.  
  134.     /* close file */
  135.     if (fclose(fp) == EOF) {
  136.         CBEPRINT;
  137.         return -1;
  138.     }
  139.  
  140.     /* notify if illegal duplicate key */
  141.     if (dupflag) {
  142.         errno = CBEDUP;
  143.         return -1;
  144.     }
  145.  
  146.     errno = 0;
  147.     return 0;
  148. }
  149.